package com.symaster.common.swing.dialog;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.symaster.common.swing.config.AppConfig;
import com.symaster.common.swing.element.MyInput;
import com.symaster.common.swing.style.Style;
import com.symaster.common.swing.util.SwingUtil;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class OperationDialog extends JDialog {

    public static final int TYPE_INPUT_DIALOG = 0;
    public static final int TYPE_ERROR_MESSAGE_DIALOG = 1;
    public static final int TYPE_WARNING_MESSAGE_DIALOG = 2;
    public static final int TYPE_INFO_MESSAGE_DIALOG = 3;

    public static final int OPERATION_YES = 0;
    public static final int OPERATION_YES_NO = 1;
    public static final int OPERATION_NO = 2;

    private int operationCode = 2;
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JPanel body_panel;
    private String message;
    private MyInput myInput;

    public OperationDialog() {
        // $$$setupUI$$$();
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        initPage();
        initListener();
    }

    /**
     * 打开对话框
     *
     * @return int | 1:点击了"确认", 2:点击了"取消"或直接关闭窗口
     */
    public static ResultEntity open(Integer type, Integer operation, String title, String message) {
        OperationDialog operationDialog = new OperationDialog();
        operationDialog.setTitle(title);
        operationDialog.setMessage(message);
        operationDialog.setBodyType(type);
        operationDialog.setOperationType(operation);
        operationDialog.pack();
        operationDialog.setVisible(true);
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.operationCode = operationDialog.getOperationCode();
        if (1 == operationDialog.getOperationCode() && TYPE_INPUT_DIALOG == type && operationDialog.myInput != null) {
            resultEntity.inputMessage = operationDialog.myInput.getValue();
        }
        return resultEntity;
    }

    public int getOperationCode() {
        return operationCode;
    }

    private void setBodyType(Integer type) {
        if (type == null) {
            type = 3;
        }
        if (TYPE_INPUT_DIALOG == type) {
            myInput = new MyInput(message, false);
            myInput.addEnterKeyListener(value -> buttonOK.doClick());
            body_panel.setPreferredSize(new Dimension(28, 500));
            body_panel.setLayout(new BorderLayout());
            body_panel.add(myInput);
        } else if (TYPE_ERROR_MESSAGE_DIALOG == type) {
            JTextField text = SwingUtil.getText(message);
            text.setDisabledTextColor(AppConfig.getColor("error_color"));
            text.setHorizontalAlignment(0);
            text.setBackground(new Color(36, 41, 46));

            body_panel.setLayout(new BorderLayout());
            body_panel.add(text);
        } else if (TYPE_INFO_MESSAGE_DIALOG == type) {
            JTextField text = SwingUtil.getText(message);
            text.setBackground(new Color(36, 41, 46));
            text.setHorizontalAlignment(0);
            body_panel.setPreferredSize(new Dimension(28, 500));
            body_panel.setLayout(new BorderLayout());
            body_panel.add(text);
        }
    }

    private void setOperationType(Integer operation) {
        if (operation == null) {
            operation = 2;
        }
        if (OPERATION_YES == operation) {
            buttonOK.setVisible(true);
            buttonCancel.setVisible(false);
        } else if (OPERATION_YES_NO == operation) {
            buttonOK.setVisible(true);
            buttonCancel.setVisible(true);
        } else {
            buttonOK.setVisible(false);
            buttonCancel.setVisible(true);
        }
    }

    private void setMessage(String message) {
        this.message = message;
    }

    private void initPage() {
        int width = 550;
        int height = 110;
        Dimension dimension = new Dimension(width, height);
        setPreferredSize(dimension);
        setMinimumSize(dimension);
        SwingUtil.toCenter(this);
        Dimension dimension1 = new Dimension(80, 20);
        Style.setBorder(1, buttonOK, buttonCancel);
        Style.setFont(1, buttonOK, buttonCancel);
        buttonOK.setPreferredSize(dimension1);
        buttonCancel.setPreferredSize(dimension1);
    }

    private void initListener() {
        buttonOK.addActionListener(e -> onOK());
        buttonCancel.addActionListener(e -> onCancel());
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });
        contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        // add your code here
        this.operationCode = 1;
        dispose();
    }

    {
        // GUI initializer generated by IntelliJ IDEA GUI Designer
        // >>> IMPORTANT!! <<<
        // DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1));
        contentPane.setBackground(new Color(-14407378));
        contentPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        panel1.setBackground(new Color(-14407378));
        contentPane.add(panel1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false));
        panel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        final Spacer spacer1 = new Spacer();
        panel1.add(spacer1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1, true, false));
        panel2.setBackground(new Color(-14407378));
        panel1.add(panel2, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        panel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        buttonOK = new JButton();
        buttonOK.setBackground(new Color(-13024950));
        buttonOK.setFocusPainted(false);
        buttonOK.setForeground(new Color(-8001379));
        buttonOK.setText("确定");
        panel2.add(buttonOK, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        buttonCancel = new JButton();
        buttonCancel.setBackground(new Color(-13024950));
        buttonCancel.setFocusPainted(false);
        buttonCancel.setForeground(new Color(-8001379));
        buttonCancel.setText("取消");
        panel2.add(buttonCancel, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        body_panel = new JPanel();
        body_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        body_panel.setBackground(new Color(-14407378));
        contentPane.add(body_panel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        body_panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
    }

    /** @noinspection ALL */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    public static class ResultEntity {
        private Integer operationCode;
        private String inputMessage;

        public Integer getOperationCode() {
            return operationCode;
        }

        public String getInputMessage() {
            return inputMessage;
        }

        @Override
        public String toString() {
            return "ResultEntity{" +
                    "operationCode=" + operationCode +
                    ", inputMessage='" + inputMessage + '\'' +
                    '}';
        }
    }
}
